# coding=utf8
from django.db import models

import datetime
from django.contrib import admin
# Create your models here.

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):
        return self.question
    def was_published_today(self):
        return self.pub_date.date() == datetime.date.today()
    was_published_today.short_description = u'Publié aujourd\'hui ?'

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()
    def __unicode__(self):
        return self.choice
    
class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 3
    
class PollAdmin(admin.ModelAdmin):
    fieldsets = [
                 (None,{'fields':['question']}),
                 ('Date de publication',{'fields':['pub_date'], 'classes': ['collapse']}),
                 ]
    list_display = ('question', 'pub_date', 'was_published_today')
    list_filter = ['pub_date']
    inlines = [ChoiceInline]

class ChoiceAdmin(admin.ModelAdmin):
    fieldsets = [
                 (None,{'fields':['poll']}),
                 ('Informations',{'fields':['choice','votes']}),
                 ]
